#include <SoftwareSerial.h>

//const int buttonPin = 5;     // the number of the pushbutton pin
const int ledPin = 4;      // the number of the LED pin
//const int neighborA_Rx_Pin = 2;
//const int neighborA_Tx_Pin = 3;
SoftwareSerial mySerial_1(6, 7); // RX, TX
SoftwareSerial mySerial_2(8, 9);
SoftwareSerial mySerial_3(10, 11);
SoftwareSerial mySerial_4(12, 13);

// variables will change:
int curButtonState = 0;         // variable for reading the pushbutton status
int preButtonState = 0;
int buttonPressed = 0;
int curNeighborAState = 0;
int preNeighborAState = 0;
int neighborA_Activated = 0;
int fadeValue = 2;
int fadeUp = 1;
int count = 0;
long whoseOnFirst;
int reset = 0; // need a reset variable for looping
int collecting_IDs; // flag to tell program to collect neighbor IDs
uint8_t IDs[16]; // array to hold IDs
uint8_t ID; //holds a single ID temporarily
int id_count = 0; //holds the position of the next open spot in the array
int noNewID = 0; //holds the amount of loops since the chip last recieved a new ID
/*void fade_led(int ledPin, int fadeValue){
  
}*/
void blink_led(int ledPin){
  delay(250);
  digitalWrite(ledPin, HIGH);
  delay(250);
  digitalWrite(ledPin, LOW);
  delay(250);
  digitalWrite(ledPin, HIGH);
  delay(250);
  digitalWrite(ledPin, LOW);
  delay(250);
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
}

//https://stackoverflow.com/questions/15094834/check-if-a-value-exist-in-a-array
int isvalueinarray(byte val, byte *arr, int size){
    int i;
    for (i=0; i < size; i++) {
        if (arr[i] == val)
            return 1;
    }
    return 0;
}

//Write ID buffer to serial TX
void writebuffertoserial(uint8_t *arr, int arr_size, int mySerial) {
  int x;
<<<<<<< HEAD
  switch(mySerial) {
    case 1 :
      for(x = 0; x < arr_size; ++x) {
        mySerial_1.write(arr[x]);
      }
    case 2 :
      for(x = 0; x < arr_size; ++x) {
       mySerial_2.write(arr[x]);
      }
    case 3 :
      for(x = 0; x < arr_size; ++x) {
       mySerial_3.write(arr[x]);
      }
     case 4 :
      for(x = 0; x < arr_size; ++x) {
        mySerial_4.write(arr[x]);
      }
=======
  for(x = 0; x < size; ++x) {
    mySerial_1.write(arr[x]);
  }
}
void writebuffertoserial_2(uint8_t *arr, int size) {
  int x;
  for(x = 0; x < size; ++x) {
    mySerial_2.write(arr[x]);
  }
}
void writebuffertoserial_3(uint8_t *arr, int size) {
  int x;
  for(x = 0; x < size; ++x) {
    mySerial_3.write(arr[x]);
  }
}
void writebuffertoserial_4(uint8_t *arr, int size) {
  int x;
  for(x = 0; x < size; ++x) {
    mySerial_4.write(arr[x]);
>>>>>>> origin/master
  }
}

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  //pinMode(neighborA_Tx_Pin, OUTPUT);
  // initialize the pushbutton pin as an input:
  //pinMode(buttonPin, INPUT);
  //pinMode(neighborA_Rx_Pin, INPUT);
<<<<<<< HEAD
  randomSeed(analogRead(0));
=======
  randomSeed(analogRead(A5));
>>>>>>> origin/master
  whoseOnFirst = random(100);
  IDs[0] = uint8_t(whoseOnFirst);
  id_count += 1;
  collecting_IDs = 1;
  // all boards start high
  digitalWrite(ledPin, LOW);
<<<<<<< HEAD
}

void loop(){
  delay(5000);
=======
  
}

void loop(){
  delay(10000);
>>>>>>> origin/master
  //determine which board is the "First"
  /*if(collecting_IDs){
    /*get other board numbers;
    if my number is less than any other
<<<<<<< HEAD
    then I am not first;*/
    writebuffertoserial(IDs, 16, 1);
=======
    then I am not first;
    writebuffertoserial_1(IDs, 16);
>>>>>>> origin/master
    while(mySerial_1.peek()){
      ID = mySerial_1.read();
      if(isvalueinarray(ID, IDs, 25) == 0){
        IDs[id_count] = ID;
        id_count += 1;
        noNewID = 0;
      } else {
        noNewID += 1;
      }
    }
    writebuffertoserial(IDs, 16, 2);
    while(mySerial_2.peek()){
      ID = mySerial_2.read();
      if(isvalueinarray(ID, IDs, 25) == 0){
        IDs[id_count] = ID;
        id_count += 1;
        noNewID = 0;
      } else {
        noNewID += 1;
      }
    }
    writebuffertoserial(IDs, 16, 3);
    while(mySerial_3.peek()){
      ID = mySerial_3.read();
      if(isvalueinarray(ID, IDs, 25) == 0){
        IDs[id_count] = ID;
        id_count += 1;
        noNewID = 0;
      } else {
        noNewID += 1;
      }
    }
    writebuffertoserial(IDs, 16, 4);
    while(mySerial_4.peek()){
      ID = mySerial_4.read();
      if(isvalueinarray(ID, IDs, 25) == 0){
        IDs[id_count] = ID;
        id_count += 1;
        noNewID = 0;
      } else {
        noNewID += 1;
      }
<<<<<<< HEAD
    }
    if(noNewID >= 40){ //This number is arbitrary
      collecting_IDs = 0;
      int i;
      for(i = 1; i < sizeof(IDs); ++i) {
        if (IDs[0] < IDs[i]) {
          digitalWrite(ledPin, HIGH);
          break;
        }
      } 
    }
=======
    }*/
    //if(noNewID >= 4000){ //This number is arbitrary
      //collecting_IDs = 0;
      //int i;
      //for(i = 1; i < sizeof(IDs); ++i) {
        //if (IDs[0] < IDs[i]) {
          digitalWrite(ledPin, HIGH);
          delay(5000);
          //break;
        //}
      //} 
    //}
>>>>>>> origin/master
//  } else {
//    // read the state of the pushbutton value:
//    curButtonState = digitalRead(buttonPin);
//    curNeighborAState = digitalRead(neighborA_Rx_Pin);
//    
//    if(neighborA_Activated != 1 && buttonPressed != 1){
//      count += 1;
//      if((count % 150) == 0)fadeValue += fadeUp;
//      if(fadeValue > 254 || fadeValue < 1){
//        fadeUp = fadeUp*(-1);
//        fadeValue += 2*fadeUp;
//      }
//      analogWrite(ledPin, fadeValue);
//    }
//    if(curNeighborAState == HIGH && preNeighborAState == LOW){
//      neighborA_Activated = 1;
//      blink_led(ledPin);
//    }
//    // check if the pushbutton is pressed.
//    if (curButtonState == HIGH && preButtonState == LOW) {     
//      //set buttonpressed
//      buttonPressed = 1;
//      // turn LED on:    
//      blink_led(ledPin);
//      digitalWrite(neighborA_Tx_Pin, HIGH);
//    } else {
//      // turn LED off:
//      //digitalWrite(ledPin, LOW); 
//      digitalWrite(neighborA_Tx_Pin, LOW);
//    }
//    preButtonState = curButtonState;
//    preNeighborAState = curNeighborAState;
<<<<<<< HEAD
  }
=======

  //}
>>>>>>> origin/master
}
